package cn.iocoder.yudao.module.trade.service.brokerage;

import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.log.BrokerageLogPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.log.BrokerageLogSaveReqVO;
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.log.AppBrokerageLogPageReqVO;
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.log.AppBrokerageLogRespVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.trade.dal.mysql.brokerage.BrokerageLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;

/**
 * 佣金操作日志 Service 实现类
 *
 * @author JackChen
 */
@Service
@Validated
public class BrokerageLogServiceImpl implements BrokerageLogService {

    @Resource
    private BrokerageLogMapper brokerageLogMapper;

    @Override
    public Long createBrokerageLog(BrokerageLogSaveReqVO createReqVO) {
        // 插入
        BrokerageLogDO brokerageLog = BeanUtils.toBean(createReqVO, BrokerageLogDO.class);
        brokerageLogMapper.insert(brokerageLog);
        // 返回
        return brokerageLog.getId();
    }

    @Override
    public void updateBrokerageLog(BrokerageLogSaveReqVO updateReqVO) {
        // 校验存在
        validateBrokerageLogExists(updateReqVO.getId());
        // 更新
        BrokerageLogDO updateObj = BeanUtils.toBean(updateReqVO, BrokerageLogDO.class);
        brokerageLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteBrokerageLog(Long id) {
        // 校验存在
        validateBrokerageLogExists(id);
        // 删除
        brokerageLogMapper.deleteById(id);
    }

    private void validateBrokerageLogExists(Long id) {
        if (brokerageLogMapper.selectById(id) == null) {
            throw exception(BROKERAGE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public BrokerageLogDO getBrokerageLog(Long id) {
        return brokerageLogMapper.selectById(id);
    }

    @Override
    public PageResult<BrokerageLogDO> getBrokerageLogPage(BrokerageLogPageReqVO pageReqVO) {
        return brokerageLogMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<BrokerageLogDO> getAppBrokerageLogPage(AppBrokerageLogPageReqVO pageReqVO) {
        return brokerageLogMapper.selectPage(pageReqVO);
    }
}